import Vue from 'vue'
// 引入axios
import Axios from 'axios'
import router from './../router'
import Store from '../store/index.js'
import RequestApi from './RequestApi.js'
import StringUtils from '../utils/StringUtils.js'
import NProgress from 'nprogress'
Vue.prototype.Axios = Axios

const CancelToken = Axios.CancelToken
const source = CancelToken.source()
const url = '/api'
/**
 *
 */
var uploadData = function (uri, data) {
  return Axios.post(url + uri, data, {
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  })
}

var uploadFile = function (uri, files, data) {
  let fileData = new FormData()
  let arrFile = []
  for (let file in files) {
    arrFile.push(file)
  }
  if (arrFile.length > 0) {
    fileData.append('file', arrFile)
  }
  for (let obj of data) {
    fileData.append(obj, data[obj])
  }
  return Axios.post(url + uri, fileData, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'responseType': 'json'

    }
  })
}
/**
 * 请求拦截
 * @param {Object} config
 */
Axios.interceptors.request.use(function (config) {
  NProgress.start()
  config.headers['language'] = localStorage.getItem('language')
  if (StringUtils.verifyTheSame(config.url, url + RequestApi.login)) {
    return config
  }
  if (StringUtils.verifyTheSame(config.url, url + RequestApi.config)) {
    return config
  }
  if (StringUtils.isEmpty(Store.state.token)) {
    source.cancel()
    router.push({
      name: 'login'
    })
  } else {
    config.headers['token'] = Store.state.token
    return config
  }
}, function (errer) {
  console.log('errer', errer)
  NProgress.done()
  return { code: 500, msg: '异常' + errer.messages, data: '' }
})
/**
 * 响应拦截
 * @param {Object} config
 */
Axios.interceptors.response.use(function (response) {
  NProgress.done()
  console.log(response.config.url)
  if (response.status === RequestApi.http_ok) {
    if (!StringUtils.verifyTheSame(response.config.url, url + RequestApi.config)) {
      Store.state.token = response.headers.token
      if (StringUtils.verifyTheSame(response.data.code, RequestApi.token_expired) ||
        StringUtils.verifyTheSame(response.data.code, RequestApi.token_invalid)
      ) {
        Store.commit('delState')
        router.resetRouter()
        router.push({ name: 'login' })
      }
    }

    return response.data
  } else if (response.status === RequestApi.errer) {

  }
  return response.data
}, function (errer) {
  NProgress.done()
  throw new Error(errer)
})

function getLeftTreeListUser () {
  let LeftTree = uploadData(RequestApi.getLeftTreeListUser, {})
  Store.state.navigations = LeftTree.data
}

// function getRoleByUser () {
//   let roleList = uploadData(RequestApi.getRoleByUser, {})
//   let roles = []
//   if (StringUtils.verifyTheSame(roleList.code, RequestApi.ok)) {
//     for (let role in roleList.data) {
//       roles.push(role.roleType)
//     }
//     Store.commit('roles', roles)
//   }
// }

function getConfig () {
  return uploadData(RequestApi.config, {})
}

/**
 * 本地图片加载
 * @param {Object} obj
 */

let requireImg = function (obj) {
  return require("'" + obj + "'")
}

export default {
  uploadData,
  uploadFile,
  getLeftTreeListUser,
  getConfig,
  requireImg
}
